﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Reflection;
using System.Net.NetworkInformation;
using RkLibs.Log;
namespace RkLibs.Functions
{
    public class CommonFunction
    {
        private static string ClassName = MethodBase.GetCurrentMethod().ReflectedType.FullName;

        public static bool CreateDirectory(string path)
        {
            bool bRet = true;
            if(!Directory.Exists(path))
            {
                try
                {
                    Directory.CreateDirectory(path);

                }
                catch(Exception ex)
                {
                    LogMgr.WriteLog(ClassName,path, ex, LogLevel.ERROR);
                    bRet = false;
                }
            }
            return bRet;
        }

        public static bool DeleteFile(string path)
        {
            try
            {
                File.Delete(path);
            }
            catch (Exception ex)
            {
                LogMgr.WriteLog(ClassName, path, ex, LogLevel.ERROR);
                return false;
            }
            return true;
        }

        public static string GetMatchStr(string input, string pattern)
        {
            Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
            if (m != null)
            {
                return m.Value;
            }
            return "";
        }

        public static bool IsNetworkAvailable()
        {
            return NetworkInterface.GetIsNetworkAvailable();
        }
        public static string GetChineseSpell(string str)
        {
            string strRet = ChineseSpellConvert.Get(str);
            return strRet;
        }
        public static void SetDoubleBuffered(object LstView)
        {
            var type = LstView.GetType();
            var info = type.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            info.SetValue(LstView, true, null);
        }
    }
}
